package com.zhny.aiot.gdsfj.plugin.model;

import com.zhny.aiot.plugin.modbus.device.ModemDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.MessageEntry;
import com.zhny.aiot.plugin.model.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;

public class FerilizerTableResultModel extends FertilizerResultModel {
    private final Logger logger = LoggerFactory.getLogger(FerilizerTableResultModel.class);
    public FerilizerTableResultModel(ModemResponseMessage modemBusResponse, ModemDevice device) {
        super (modemBusResponse,device);
    }

    @Override
    protected void onDecoder(MessageEntry entry) {
        logger.info("---fertilizer writer table message---");
        int len = getModemBusResponse().getSize();
        if(len == 23*2){
            FertilizerStoreEntry storeEntry = new FertilizerStoreEntry();
            DecimalFormat decimalFormat = new DecimalFormat("#.00");
            storeEntry.setPh(Float.parseFloat(decimalFormat.format(getModemBusResponse().getData().readFloat())));
            storeEntry.setEc(Float.parseFloat(decimalFormat.format(getModemBusResponse().getData().readFloat())));
            storeEntry.setPressure(Float.parseFloat(decimalFormat.format(getModemBusResponse().getData().readFloat())));
            getModemBusResponse().getData().skipBytes(13*2);
            storeEntry.setTotalFlow(Float.parseFloat(decimalFormat.format(getModemBusResponse().getData().readFloat())));
            storeEntry.setMainFlowValue(Float.parseFloat(decimalFormat.format(getModemBusResponse().getData().readFloat())));
            entry.setData(storeEntry);
            getDevice().saveReal(storeEntry);
            getDevice().save(storeEntry);
        }
        else{
            entry.setType(MessageType.ERROR);
            entry.setData("response length error !");
        }
        getDevice().nofity(entry);
    }

}
